clear all
close all
load XYZ;
load myD65;
load Fabric;
XYZ=data;
rangeXYZ=XYZ(11:end,:);
% spectrophotometer measurements were taken for wavelengths between 380 and
% 730 every 4nm, so we are adjusting the rest of the data, matrices, etc to
% be within the same range.
rangeXYZ=rangeXYZ(1:4:end,:);
waverange=wavelength(1:88); %wavelength is from Fabric.mat
D65light=D65.D65light;
% the XYZ to sRGB matrix
XYZ2sRGB=[3.1406 -1.5372 -.4986; -.9689 1.8758 .0415; .0557 -.2040 1.057];

illuminantSPD = spectral(1:88,2);
fabricSPD = spectral(1:88,27);

handle = plot(waverange,fabricSPD);
set(handle,'Color','m','Linewidth',3);
grid on
hold on
handle2 = plot(waverange,illuminantSPD);
set(handle2,'Color','b','Linewidth',3);
legend('Fabric under Illuminant','Illuminant')
xlabel('Wavelength (nm)')
ylabel('Intensity')

fabricSPDlight = D65light'.*fabricSPD./illuminantSPD;
fabricXYZ=rangeXYZ'*fabricSPDlight;
fabricRGB=XYZ2sRGB*fabricXYZ;

figure
handle = plot(waverange, fabricSPD./illuminantSPD);
set(handle,'Color','m', 'Linewidth', 3);
grid on
legend('Fabric Reflectance Spectrum');
xlabel('Wavelength (nm)')
ylabel('Intensity')

figure
handle = plot(waverange, fabricSPDlight);
set(handle,'Color','m','Linewidth',3);
grid on
hold on
handle2 = plot(waverange, D65light);
set(handle2,'Color','b','Linewidth',3);
legend('Fabric Reflectance Spectrum under Daylight', 'Daylight D65 Spectrum');
xlabel('Wavelength (nm)')
ylabel('Intensity')

fabricIm = imread('tangerine_alpine.jpg');
fabricIm = imread('fleece_pink.jpg');
croppedImage=fabricIm(30:60,30:50,:);
fabricImR=croppedImage(:,:,1);
fabricImG=croppedImage(:,:,2);
fabricImB=croppedImage(:,:,3);

fabricImR=mean(mean(fabricImR));
fabricImG=mean(mean(fabricImG));
fabricImB=mean(mean(fabricImB));

idealColor=croppedImage;

% the scaling factor found by getting RGB values
% for D65 light. The maximum value, G, was 2641.6
% so we are scaling by 2.6415 such that resulting
% RGB is between 1 and 1000 - the suitable range
% for the input into the inverse gamma table
maxRawRGB = 2.6416;

idealRawR=fabricRGB(1)/maxRawRGB;
idealRawG=fabricRGB(2)/maxRawRGB;
idealRawB=fabricRGB(3)/maxRawRGB;

% keeping track of whether the RGB values had to be 
% truncated 
OUTOFBOUNDS=0;
NEGATIVERGB=0;
if(idealRawR>1000 || idealRawG>1000 || idealRawB>1000)
    OUTOFBOUNDS = 1
end
if(idealRawR<0 || idealRawG < 0 || idealRawB<0)
    NEGATIVERGB = 1
end

 idealColorR=min(1000,idealRawR);
 idealColorG=min(1000,idealRawG);
 idealColorB=min(1000,idealRawB);
 
 idealColorR=max(1,idealColorR);
 idealColorG=max(1,idealColorG);
 idealColorB=max(1,idealColorB);
 
% convert from linear sRGB to digital sRGB for the frame buffer
load monitorGam
invGamTable = mkInvGammaTable(monitorGam,1000);
idealR = round(invGamTable(round(idealColorR),1));
idealG = round(invGamTable(round(idealColorG),2));
idealB = round(invGamTable(round(idealColorB),3));

idealColor(:,:,1)=idealR; 
idealColor(:,:,2)=idealG; 
idealColor(:,:,3)=idealB; 


figure
%subplot(2,1,1);
imshow(idealColor)
%subplot(2,1,2);
%imshow(croppedImage)

pictureColors = [fabricImR fabricImG fabricImB]
colorBeforenAfterGamma = [idealRawR idealRawG idealRawB;
    idealR idealG idealB]
OUTOFBOUNDS
NEGATIVERGB
